#include <iostream>
#include <string>
#include <unordered_map>
using std::cout;
using std::endl;
using std::string;
using std::unordered_map;

// 初始bucket_count大小1
// 默认装载因子max_load_factor 1.0，达到时bucket_count扩展为1.5倍
// JAVA HashMap解决冲突：桶内元素个数<=8链表，>8红黑树
void TestUnorderedMap() {
  unordered_map<string, string> map0;
  cout << "map0 bucket_count:" << map0.bucket_count()
       << ", max_bucket_count:" << map0.max_bucket_count() << endl;

  unordered_map<string, string> map1 = {
      {"house", "maison"}, {"apple", "pomme"}, {"tree", "arbre"},
      {"book", "livre"},   {"door", "porte"},  {"grapefruit", "pamplemousse"}};
  map1.insert({"tree", "arbre2"});

  cout << "size:" << map1.size() << endl;
  cout << "bucket_count:" << map1.bucket_count() << endl;

  for (unordered_map<string, string>::size_type i = 0; i < map1.bucket_count();
       ++i) {
    cout << "bucket#" << i << " ";
    for (auto it = map1.begin(i); it != map1.end(i); ++it) {
      cout << it->first << ":" << it->second << ", ";
    }
    cout << endl;
  }

  for (auto& item : map1) {
    cout << item.first << ' ';
  }
  cout << endl;
}

int main() { TestUnorderedMap(); }